package com.xhwh.wx.orm.service;

import com.xhwh.wx.orm.model.Artist;
import org.hibernate.SessionFactory;
import org.hibernate.classic.Session;

import java.util.Collection;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * Created by zsq on 3/20/15.
 */
public class ArtistService {

    public SessionFactory getDbsession() {
        return dbsession;
    }

    public void setDbsession(SessionFactory dbsession) {
        this.dbsession = dbsession;
    }

    private SessionFactory dbsession;

    private final AtomicInteger counter = new AtomicInteger();


    public Artist getArtist()
    {

        Session session = getDbsession().openSession();
        session.beginTransaction();

        List result = session.createQuery("from Artist").list();
        session.getTransaction().commit();
        session.close();
        return (Artist)result.get(counter.incrementAndGet()%result.size());

    }

    public Collection getAllArtists()
    {

        Session session = getDbsession().openSession();
        session.beginTransaction();

        List result = session.createQuery("from Artist").list();
        session.getTransaction().commit();
        session.close();
        return result;
    }


}
